<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>CAD二次开发基础 | 苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="CAD二次开发基础   CAD二次开发基础 示例代码     CAD开发中基本概念 每个CAD文件打开的文件在CAD中就是一个Document  Application.DocumentManager.MdiActiveDocument 表示当前激活的窗口 SendStringToExecute是一个异步方法,可以使用CommandEnded +&#x3D; MethodHandler,来进行回调  示例代">
<meta property="og:type" content="article">
<meta property="og:title" content="CAD二次开发基础">
<meta property="og:url" content="http://dontworryneedmoney.gitee.io/guolianbo/2019/09/04/dontworry/%E6%9D%82%E9%A1%B9/CAD%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒">
<meta property="og:description" content="CAD二次开发基础   CAD二次开发基础 示例代码     CAD开发中基本概念 每个CAD文件打开的文件在CAD中就是一个Document  Application.DocumentManager.MdiActiveDocument 表示当前激活的窗口 SendStringToExecute是一个异步方法,可以使用CommandEnded +&#x3D; MethodHandler,来进行回调  示例代">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2019-09-03T17:42:53.000Z">
<meta property="article:modified_time" content="2019-11-10T08:28:53.955Z">
<meta property="article:author" content="worry dont">
<meta property="article:tag" content="杂项">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/guolianbo/atom.xml" title="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/guolianbo/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/guolianbo/" id="logo">苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/guolianbo/">Home</a>
        
          <a class="main-nav-link" href="/guolianbo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/guolianbo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://dontworryneedmoney.gitee.io/guolianbo"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="ejs-dontworry/杂项/CAD二次开发基础" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2019/09/04/dontworry/%E6%9D%82%E9%A1%B9/CAD%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91%E5%9F%BA%E7%A1%80/" class="article-date">
  <time datetime="2019-09-03T17:42:53.000Z" itemprop="datePublished">2019-09-04</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      CAD二次开发基础
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="CAD二次开发基础"><a href="#CAD二次开发基础" class="headerlink" title="CAD二次开发基础"></a>CAD二次开发基础</h1><!-- TOC -->

<ul>
<li><a href="#cad二次开发基础">CAD二次开发基础</a><ul>
<li><a href="#示例代码">示例代码</a></li>
</ul>
</li>
</ul>
<!-- /TOC -->
<h2 id="CAD开发中基本概念"><a href="#CAD开发中基本概念" class="headerlink" title="CAD开发中基本概念"></a>CAD开发中基本概念</h2><ul>
<li>每个CAD文件打开的文件在CAD中就是一个Document </li>
<li>Application.DocumentManager.MdiActiveDocument 表示当前激活的窗口</li>
<li>SendStringToExecute是一个异步方法,可以使用CommandEnded += MethodHandler,来进行回调</li>
</ul>
<h2 id="示例代码"><a href="#示例代码" class="headerlink" title="示例代码"></a>示例代码</h2><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 打开指定路径下的CAD文件</span></span><br><span class="line">[<span class="meta">CommandMethod(<span class="meta-string">"OpenDWGDXF"</span>, CommandFlags.Session)</span>]</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">OpenDWGDXF</span>(<span class="params"><span class="keyword">string</span> currentFile</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">try</span></span><br><span class="line">    &#123;</span><br><span class="line">        _currentFile = currentFile;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">string</span>.IsNullOrEmpty(_currentFile))</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">string</span> strFileName = _currentFile;</span><br><span class="line">        DocumentCollection acDocMgr = Application.DocumentManager;</span><br><span class="line">        Document doc = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">if</span> (File.Exists(strFileName))</span><br><span class="line">        &#123;</span><br><span class="line">            doc = acDocMgr.Open(strFileName, <span class="literal">false</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">            acDocMgr.MdiActiveDocument.Editor.WriteMessage(<span class="string">"File "</span> + strFileName + <span class="string">" does not exist."</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (doc == <span class="literal">null</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        acDocMgr.MdiActiveDocument = doc;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span> (Exception ex)</span><br><span class="line">    &#123;</span><br><span class="line">        ShowMsg(ex);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 删除指定类型的图元</span></span><br><span class="line">[<span class="meta">CommandMethod(<span class="meta-string">"DeleteTextDimension"</span>, CommandFlags.Modal)</span>]</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">DeleteTextDimension</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Document doc = Application.DocumentManager.MdiActiveDocument;</span><br><span class="line">    <span class="keyword">if</span> (doc == <span class="literal">null</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    Editor ed = doc.Editor;</span><br><span class="line">    TypedValue[] tvs =</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">new</span> TypedValue((<span class="keyword">int</span>) DxfCode.Operator, <span class="string">"&lt;or"</span>),</span><br><span class="line">        <span class="keyword">new</span> TypedValue((<span class="keyword">int</span>) DxfCode.Start, <span class="string">"TEXT"</span>),</span><br><span class="line">        <span class="keyword">new</span> TypedValue((<span class="keyword">int</span>) DxfCode.Start, <span class="string">"DIMENSION"</span>),</span><br><span class="line">        <span class="keyword">new</span> TypedValue((<span class="keyword">int</span>) DxfCode.Operator, <span class="string">"or&gt;"</span>)</span><br><span class="line">    &#125;;</span><br><span class="line">    SelectionFilter sf = <span class="keyword">new</span> SelectionFilter(tvs);</span><br><span class="line">    PromptSelectionResult psr = ed.SelectAll(sf);</span><br><span class="line">    <span class="keyword">using</span> (Transaction trans = doc.TransactionManager.StartTransaction())</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (psr.Status == PromptStatus.OK)</span><br><span class="line">        &#123;</span><br><span class="line">            SelectionSet SS = psr.Value;</span><br><span class="line">            ObjectId[] idArray = SS.GetObjectIds();</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; idArray.Length; i++)</span><br><span class="line">            &#123;</span><br><span class="line">                Entity ent = trans.GetObject(idArray[i], OpenMode.ForWrite) <span class="keyword">as</span> Entity;</span><br><span class="line">                ent.Erase(<span class="literal">true</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        trans.Commit();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 导出DXF格式的文件</span></span><br><span class="line">[<span class="meta">CommandMethod(<span class="meta-string">"OutDxf"</span>, CommandFlags.Session)</span>]</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">OutDxf</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Document doc = Application.DocumentManager.MdiActiveDocument;</span><br><span class="line">    <span class="keyword">if</span> (doc == <span class="literal">null</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">string</span> strDWGName = doc.Name;</span><br><span class="line">    <span class="keyword">object</span> obj = Application.GetSystemVariable(<span class="string">"DWGTITLED"</span>);</span><br><span class="line">    <span class="keyword">string</span> fileName = Path.GetFileNameWithoutExtension(strDWGName);</span><br><span class="line">    strDWGName = OutDirectory + fileName + <span class="string">".dxf"</span>;</span><br><span class="line">    <span class="keyword">this</span>._upLoadFilePath = strDWGName;</span><br><span class="line">    doc.Database.DxfOut(strDWGName, <span class="number">16</span>, DwgVersion.Current);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 关闭除当前窗口外的其他窗口</span></span><br><span class="line">[<span class="meta">CommandMethod(<span class="meta-string">"CloseDwgs"</span>)</span>]</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">CloseDwgs</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">var</span> dwg = Application.DocumentManager.MdiActiveDocument;</span><br><span class="line">    <span class="keyword">foreach</span> (Document d <span class="keyword">in</span> Application.DocumentManager)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (!d.IsActive &amp;&amp; d.UnmanagedObject != dwg.UnmanagedObject)</span><br><span class="line">        &#123;</span><br><span class="line">            d.CloseAndDiscard();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    dwg.CommandEnded += StartUpLoadFile;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2019/09/04/dontworry/%E6%9D%82%E9%A1%B9/CAD%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91%E5%9F%BA%E7%A1%80/" data-id="ckb6hw6rt001hjcn28dnn3krm" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/guolianbo/2019/09/12/dontworry/%E6%9D%82%E9%A1%B9/CSharp%E7%9A%84%E7%BB%86%E8%8A%82/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          CSharp的细节
        
      </div>
    </a>
  
  
    <a href="/guolianbo/2019/09/04/dontworry/%E6%9D%82%E9%A1%B9/C#HTTP%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">C#HTTP常用方法</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/guolianbo/categories/%E9%9A%8F%E7%AC%94/">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Unity3d/" rel="tag">Unity3d</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" rel="tag">消防</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" rel="tag">申论</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" rel="tag">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/guolianbo/tags/Algorithm/" style="font-size: 14px;">Algorithm</a> <a href="/guolianbo/tags/Unity3d/" style="font-size: 12px;">Unity3d</a> <a href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" style="font-size: 18px;">杂项</a> <a href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" style="font-size: 16px;">消防</a> <a href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" style="font-size: 10px;">申论</a> <a href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" style="font-size: 20px;">随笔</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/06/">June 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/04/">April 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/03/">March 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/02/">February 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/01/">January 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/06/">June 2019</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/guolianbo/2020/06/08/dontworry/Unity3d/IObserver%E5%92%8CIObservable%E5%92%8CIDisposable/">IObserver和IObservable和IDisposable</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/28/dontworry/%E6%9D%82%E9%A1%B9/javascript%E7%9A%84%E6%9C%AC%E8%B4%A8/">javascript的本质</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/27/dontworry/%E6%9D%82%E9%A1%B9/%E5%8D%8F%E7%A8%8B%E7%9A%84%E5%8E%9F%E7%90%86/">协程的原理</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/20/dontworry/%E6%9D%82%E9%A1%B9/linux%E4%B8%8B%E7%9A%84nginx%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/">linux下的nginx的基本操作</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/15/dontworry/%E6%9D%82%E9%A1%B9/%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E5%8A%A8%E8%AF%8D/">常用函数动词</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 worry dont<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/guolianbo/" class="mobile-nav-link">Home</a>
  
    <a href="/guolianbo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/guolianbo/fancybox/jquery.fancybox.css">

  
<script src="/guolianbo/fancybox/jquery.fancybox.pack.js"></script>




<script src="/guolianbo/js/script.js"></script>




  </div>
</body>
</html>